var document_title = document.title;
var G_UNREAD_NOTIFICATION = 0;

$(document).ready(function() {
  // 检测是否有弹窗活动
  if (
    G_USER_ID &&
    (G_ROUTER == "site" ||
      G_ROUTER == "home" ||
      G_ROUTER == "explore" ||
      G_ROUTER == "question")
  ) {
    $.get(
      G_BASE_URL + "/account/ajax/get_recruits/",
      function(result) {
        if (result.errno == 1) {
          var list = result.rsm,
            index;

          if (list.length > 0) {
            for (var i = 0; i < list.length; i++) {
              // 判断缓存中是否有稍后提醒标识
              if (window.localStorage["recruits" + list[i].id] != undefined) {
                if (
                  Math.floor(new Date().getTime() / 1000) >
                  window.localStorage["recruits" + list[i].id]
                ) {
                  index = i;
                  break;
                }
              } else {
                index = i;
                break;
              }
            }

            if (index != undefined) {
              AWS.dialog("bannerActivityRecruits", {
                type: list[index].event_id,
                title: list[index].title,
                desc: list[index].description,
                imgUrl:
                  G_STATIC_URL +
                  "/dist/new/events/" +
                  list[index].event_id +
                  "/dialog-bg.png",
                link: list[index].link,
                id: list[index].id
              });
            }
          }
        }
      },
      "json"
    );
  }

  // 顶部通知条关闭
  $(".global-tips.notification .icon-delete").click(function() {
    $(".global-tips.notification").detach();
    $.cookie("global-tips-close", "true", { path: "/" });
  });

  if ($.cookie("global-tips-close") != "true") {
    $(".global-tips.notification").show();
  } else {
    $(".global-tips.notification").detach();
  }

  // demo 报告切换
  if ($(".global-tips.demo").length) {
    var sex = $(".global-tips .sex").attr("data-sex"),
      isWgs = sex.match("wgs_") == null ? false : true,
      currentSex = sex.replace("wgs_", "");

    $(".global-tips .sex").val(currentSex);

    if (isWgs) {
      $(".global-tips .version").val("wgs");
    } else {
      $(".global-tips .version").val("wegene");
    }

    $(".global-tips .version").change(function() {
      var val = $(this).val();

      if (val == "wgs") {
        window.location.href = G_BASE_URL + "/demo2/wgs_" + currentSex + "/";
      } else {
        window.location.href = G_BASE_URL + "/demo2/" + currentSex + "/";
      }
    });

    $(".global-tips .sex").change(function() {
      var val = $(this).val();

      if (isWgs) {
        window.location.href = G_BASE_URL + "/demo2/wgs_" + val + "/";
      } else {
        window.location.href = G_BASE_URL + "/demo2/" + val + "/";
      }
    });
  }

  if ($.cookie("global-tips-close") != "true") {
    $(".global-tips").show();
  } else {
    $(".global-tips").detach();
  }

  // 检测是否有积分操作or等级提升
  AWS.global_tips("integral", function() {
    AWS.global_tips("message_exp");
  });

  // 报告名字首字母
  if ($(".report-change-box").length > 0) {
    $(".report-change-box span").html(
      $(".report-change-box span")
        .attr("data-name")
        .substring(0, 1)
    );
  }

  // 报告切换
  $(".report-change-box").click(function() {
    if ($(".side-nav").hasClass("animate-show")) {
      $(".side-nav, .container")
        .addClass("animate-hide")
        .removeClass("animate-show");
    } else {
      $(".side-nav, .container")
        .addClass("animate-show")
        .removeClass("animate-hide");
    }
  });

  // 切换关闭
  $(".side-nav .bg, .side-nav .icon-right").click(function() {
    $(".side-nav, .container")
      .addClass("animate-hide")
      .removeClass("animate-show");
  });

  // 低版本ie检测
  if (
    /MSIE 6/.test(navigator.userAgent) ||
    /MSIE 7/.test(navigator.userAgent) ||
    /MSIE 8/.test(navigator.userAgent)
  ) {
    var newNode = document.createElement("div");
    newNode.setAttribute("class", "browser-not-support");
    newNode.innerHTML = document.getElementById(
      "template-browserNotSupport"
    ).innerHTML;

    document.getElementsByTagName("body")[0].appendChild(newNode);
  }

  // 导航滚动
  $(window).scroll(function() {
    if ($(window).scrollTop() >= 30) {
      $(".header").addClass("fixed");
    } else {
      $(".header").removeClass("fixed");
    }
  });

  // 导航下拉
  $(".aw-top-nav ul li").hover(
    function() {
      if ($(this).attr("data-toggle")) {
        $(
          '.header .nav-toggle .toggle[data-toggle="' +
            $(this).attr("data-toggle") +
            '"]'
        ).addClass("active");
      }
    },
    function() {
      $(
        '.header .nav-toggle .toggle[data-toggle="' +
          $(this).attr("data-toggle") +
          '"]'
      ).removeClass("active");
    }
  );

  $(".header .nav-toggle .toggle").hover(
    function() {
      $(this).addClass("active");
    },
    function() {
      $(this).removeClass("active");
    }
  );

  // 示例报告性别
  $(".aw-tips-bar.demo select").val(
    $(".aw-tips-bar.demo select").attr("data-sex")
  );

  $(".aw-tips-bar.demo select").change(function() {
    var sex = $(this).val();

    window.location.href = G_BASE_URL + "/demo2/" + sex + "/";
  });

  // 搜索框展开
  $(".aw-top-nav .search").click(function() {
    $(".header").addClass("active");

    $(".aw-user-nav, .aw-top-nav").hide();

    $(".search-box").show();
  });

  // 搜索框关闭
  $(".search-box .icon-delete").click(function() {
    $(".header").removeClass("active");

    $(".aw-user-nav, .aw-top-nav").show();

    $(".search-box").hide();
  });

  // pagehead 点击下拉

  if ($.cookie("pagehead-toggle") == "true") {
    $(".pagehead .icon-down")
      .removeClass("icon-down")
      .addClass("icon-up");
    $(".pagehead .content").show();
  }

  $(".pagehead .toggle").click(function() {
    var icon = $(this).find(".icon");

    if (icon.hasClass("icon-down")) {
      $(this)
        .parents(".pagehead")
        .find(".content")
        .slideDown();

      icon.removeClass("icon-down").addClass("icon-up");

      $.cookie("pagehead-toggle", "true", { path: "/" });
    } else {
      $(this)
        .parents(".pagehead")
        .find(".content")
        .slideUp();

      icon.removeClass("icon-up").addClass("icon-down");

      $.cookie("pagehead-toggle", "false");
    }
  });

  // pagehead 关闭
  $(".pagehead .icon-delete").click(function() {
    var icon = $(this)
      .parents(".pagehead")
      .find(".icon");

    $(this)
      .parents(".pagehead")
      .find(".content")
      .slideUp();

    icon.removeClass("icon-up").addClass("icon-down");
  });

  $(".aw-top-nav .community a, .side-nav .community a").click(function() {
    if ($.cookie("category") > 0) {
      window.location.href =
        G_BASE_URL + "/explore/?category=" + $.cookie("category");
    } else if ($.cookie("category") == 0) {
      window.location.href = G_BASE_URL + "/explore/";
    } else {
      window.location.href = G_BASE_URL + "/" + $(this).attr("data-url");
    }
  });

  // 社区分类自动跳转
  $(".aw-explore-list .setting-tabs a").click(function() {
    $.cookie("category", $(this).attr("data-id"), { path: "/" });

    window.location.href = G_BASE_URL + "/" + $(this).attr("data-url");
  });

  // 响应式菜单点击效果
  $(".header .navbar-toggle").click(function() {
    $(this)
      .parent()
      .parent()
      .find(".side-nav")
      .removeClass("animate-hide")
      .addClass("animate-show");
  });

  // 响应式菜单关闭
  $(".header .side-nav .icon-delete").click(function() {
    $(this)
      .parents(".side-nav")
      .removeClass("animate-show")
      .addClass("animate-hide");
  });

  // 响应式菜单自动关闭
  $(".header .side-nav .bg").click(function(e) {
    $(".header .side-nav").removeClass("animate-show");
  });

  $(".aw-top-nav .side-nav dl dt").click(function() {
    var dd = $(this)
      .parents("dl")
      .find("dd");

    if (dd.is(":visible")) {
      dd.slideUp();

      $(this)
        .parents("dl")
        .find(".icon")
        .removeClass("icon-up")
        .addClass("icon-down");
    } else {
      dd.slideDown();

      $(this)
        .parents("dl")
        .find(".icon")
        .removeClass("icon-down")
        .addClass("icon-up");
    }
  });

  // fix form bug...
  $("form[action='']").attr("action", window.location.href);

  // 验证码
  $("img#captcha").attr("src", G_BASE_URL + "/account/captcha/");

  // 输入框自动增高
  if ($(".autosize").length) {
    $(".autosize").autosize();
  }

  //响应式导航条效果
  $(".aw-top-nav .navbar-toggle").click(function() {
    if (
      $(this)
        .parents(".aw-top-nav")
        .find(".navbar-collapse")
        .hasClass("active")
    ) {
      $(this)
        .parents(".aw-top-nav")
        .find(".navbar-collapse")
        .removeClass("active");
    } else {
      $(this)
        .parents(".aw-top-nav")
        .find(".navbar-collapse")
        .addClass("active");
    }
  });

  //检测通知
  AWS.Message.check_notifications();

  if ($("a[rel=lightbox]:visible").length) {
    $("a[rel=lightbox]").fancybox({
      openEffect: "none",
      closeEffect: "none",
      prevEffect: "none",
      nextEffect: "none",
      centerOnScroll: true,
      closeBtn: false,
      helpers: {
        buttons: {
          position: "bottom"
        }
      },
      afterLoad: function() {
        this.title =
          "第 " +
          (this.index + 1) +
          " 张, 共 " +
          this.group.length +
          " 张" +
          (this.title ? " - " + this.title : "");
      }
    });
  }

  if (window.location.hash.indexOf("#!") != -1) {
    if ($('a[name="' + window.location.hash.replace("#!", "") + '"]').length) {
      var height = parseInt($(".header").height()) * 2;

      $.scrollTo(
        parseInt(
          $("a[name=" + window.location.hash.replace("#!", "") + "]").offset()
            .top
        ) - height,
        600,
        { queue: true }
      );
    }
  }

  //话题编辑下拉菜单click事件
  $(document).on(
    "click",
    ".aw-edit-topic-box .aw-dropdown-list li",
    function() {
      $(this)
        .parents(".aw-edit-topic-box")
        .find("#aw_edit_topic_title")
        .val($(this).text());
      $(this)
        .parents(".aw-edit-topic-box")
        .find(".add")
        .click();
      $(this)
        .parents(".aw-edit-topic-box")
        .find(".aw-dropdown")
        .hide();
    }
  );

  // Modify by wecenter
  /*icon tooltips提示*/
  $(document).on(
    "click",
    ".icon-good, .icon-bad, .follow, .voter, .aw-icon-thank-tips, .invite-list-user, .tip, .tooltips.show",
    function() {
      $(this).tooltip("toggle");
    }
  );
});

$(window).on("hashchange", function() {
  if (window.location.hash.indexOf("#!") != -1) {
    if ($("a[name=" + window.location.hash.replace("#!", "") + "]").length) {
      $.scrollTo(
        $("a[name=" + window.location.hash.replace("#!", "") + "]").offset()[
          "top"
        ] - 20,
        600,
        { queue: true }
      );
    }
  }
});
